from db import *
import pandas as pd
import pyecharts.options as opts
from pyecharts.charts import Pie
class Bookpie:
    def __init__(self):
        # 1：数据采集
        #书籍名称	借阅人姓名	班级	借阅日期	借阅天数
        #bookrecords = pd.read_csv("data/书籍借阅记录.csv", encoding='utf-8-sig')
        db = Db()
        list  = db.get_DBdata()
        bookcount = bookrecords['书籍名称'].groupby(bookrecords['书籍名称']).count()
        print(bookcount)
        # 3:数据可视化
        x_data = list(bookcount.keys())
        y_data = list(bookcount)
        data_pair = [list(z) for z in zip(x_data, y_data)]
        data_pair.sort(key=lambda x: x[1])
        (
            Pie(init_opts=opts.InitOpts(width="100%", height="100%", bg_color="#e9e2e200"))
                .add(
                series_name="访问来源",
                data_pair=data_pair,
                rosetype="radius",
                radius="55%",
                center=["50%", "50%"],
                label_opts=opts.LabelOpts(is_show=False, position="center"),
            )
                .set_global_opts(
                title_opts=opts.TitleOpts(
                    title="",
                    pos_left="center",
                    pos_top="20",
                    title_textstyle_opts=opts.TextStyleOpts(color="#fff"),
                ),
                legend_opts=opts.LegendOpts(is_show=False),
            )
                .set_series_opts(
                tooltip_opts=opts.TooltipOpts(
                    trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"
                ),
                label_opts=opts.LabelOpts(color="rgba(255, 255, 255, 0.3)"),
            )
                .render("templates/book_pie.html")
        )
Bookpie()
